package com.ccp.dev.form.model;

import com.ccp.dev.core.basic.model.BaseModel;
import com.ccp.dev.core.basic.table.TableModel;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.*;

/**
 * BPM_FORM_TABLE Model对象
 *
 * @author zhaowj
 */
@XmlRootElement(name = "table")
@XmlAccessorType(XmlAccessType.NONE)
public class FormTable extends BaseModel<FormTable>{

    /**
     * 未发布 =0
     */
    public final static Short NOT_PUBLISH = 0;

    /**
     * 发布 =1
     */
    public final static Short IS_PUBLISH = 1;

    /**
     * 标记删除：是
     */
    public final static short IS_Y = 1;
    /**
     * 标记删除：否
     */
    public final static short IS_N = 0;

    /**
     * 不是外部表=1
     */
    public final static int NOT_EXTERNAL = 0;

    /**
     * 是主表
     */
    public final static int IS_MAIN = 1;

    /**
     * 是业务数据关联表
     */
    public final static Short IS_BUSINESS_DATA_ASSOCIATION = 2;

    /**
     * 不是主表
     */
    public final static int IS_NOT_MAIN = 0;

    /**
     * 公开=1
     */
    public final static Short IS_PUBLIC = 1;

    /**
     * 需要版本控制=1
     */
    public final static Integer NEED_VER = 1;

    /**
     * 不需要版本控制
     */
    public final static Integer NEED_VER_NOT = 0;

    /**
     * tableId
     */
    @XmlAttribute
    protected String tableId;

    /**
     * 表名
     */
    @XmlAttribute
    protected String tableName;

    /**
     * 表描述
     */
    @XmlAttribute
    protected String tableDesc;

    /**
     * 是否主表 1-是 0-否
     */
    @XmlAttribute
    protected Short isMain = 1;

    /**
     * 所属主表，该字段仅针对从表
     */
    @XmlAttribute
    protected String mainTableId;

    /**
     * 是否已发布
     */
    @XmlAttribute
    protected Short isPublished = 0;

    /**
     * 发布人
     */
    protected String publishedBy;

    /**
     * 发布时间
     */
    protected java.util.Date publishTime;

    /**
     * 是否外部数据源
     */
    protected int isExternal = NOT_EXTERNAL;

    /**
     * 是否公开
     */
    protected int isPublic = IS_PUBLIC;

    /**
     * 是否需要版本控制
     */
    protected Integer needVer = NEED_VER_NOT;

    /**
     * 数据源别名
     */
    @XmlAttribute
    protected String dsAlias;

    /**
     * 数据源名称
     */
    @XmlAttribute
    protected String dsName;

    /**
     * 外键字段
     */
    protected String relation;

    /**
     * 键类型
     */
    protected Short keyType = 0;

    /**
     * 键值
     */
    protected String keyValue;

    /**
     * 主键字段
     */
    protected String pkField;

    /**
     * 列表模板
     */
    protected String listTemplate;

    /**
     * 明细模板
     */
    protected String detailTemplate;

    /**
     * 是否有表单产生(0,表设计,1,由表单生成) 默认值为0
     */
    protected Integer genByForm = 0;

    /**
     * 分组
     */
    @XmlAttribute
    protected String team;

    /**
     * 数据库主键类型。
     * 主键数据类型(0,数字,1字符串)
     */
    @XmlAttribute
    protected Short keyDataType = 1;

    /**
     * 创建人
     */
    protected String creator;

    /**
     * 分类id
     */
    @XmlAttribute
    protected String categoryId;

    /**
     * 对应的子表
     */
    protected String subStr;

    protected Integer hasForm = 0;

    /**
     * 外部表=1
     */
    public final static int EXTERNAL = 1;

    /**
     * 字段信息
     */
    protected List<FormField> fieldList = new ArrayList<>();

    /**
     * 主键类型 数字=0
     */
    public final static Short PKTYPE_NUMBER=0;

    /**
     * 主键类型 字符串=1
     */
    public final static Short PKTYPE_STRING=1;

    /**
     * 子表列表
     */
    protected List<FormTable> subTableList = new ArrayList<>();

    /**
     * 其它列表
     */
    protected List<FormTable> otherTableList = new ArrayList<>();

    /**
     * 主表描述
     */
    protected String mainTableDesc;

    /**
     * 主表名称
     */
    protected String mainTableName;
    /**
     * 查询条件字段信息（去掉加密字段）
     */
    protected List<FormField> queryFieldList = new ArrayList<>();

    //添加字段需要用到hashSet
    protected Set<String> hashSet = new HashSet<>(16);

    public List<FormField> getQueryFieldList() {
        return queryFieldList;
    }

    public void setQueryFieldList(List<FormField> queryFieldList) {
        this.queryFieldList = queryFieldList;
    }

    public String getMainTableDesc() {
        return mainTableDesc;
    }

    public void setMainTableDesc(String mainTableDesc) {
        this.mainTableDesc = mainTableDesc;
    }

    public String getMainTableName() {
        return mainTableName;
    }

    public void setMainTableName(String mainTableName) {
        this.mainTableName = mainTableName;
    }

    public List<FormTable> getSubTableList() {
        return subTableList;
    }

    public void setSubTableList(List<FormTable> subTableList) {
        this.subTableList = subTableList;
    }

    public List<FormField> getFieldList() {
        return fieldList;
    }

    public void setFieldList(List<FormField> fieldList) {
        this.fieldList = fieldList;
    }

    public String getTableId() {
        return tableId;
    }

    public void setTableId(String tableId) {
        this.tableId = tableId;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableDesc() {
        return tableDesc;
    }

    public void setTableDesc(String tableDesc) {
        this.tableDesc = tableDesc;
    }

    public Short getIsMain() {
        return isMain;
    }

    public void setIsMain(Short isMain) {
        this.isMain = isMain;
    }

    public String getMainTableId() {
        return mainTableId;
    }

    public void setMainTableId(String mainTableId) {
        this.mainTableId = mainTableId;
    }

    public Short getIsPublished() {
        return isPublished;
    }

    public void setIsPublished(Short isPublished) {
        this.isPublished = isPublished;
    }

    public String getPublishedBy() {
        return publishedBy;
    }

    public void setPublishedBy(String publishedBy) {
        this.publishedBy = publishedBy;
    }

    public Date getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }

    public int getIsExternal() {
        return isExternal;
    }

    public void setIsExternal(int isExternal) {
        this.isExternal = isExternal;
    }

    public int getIsPublic() {
        return isPublic;
    }

    public void setIsPublic(int isPublic) {
        this.isPublic = isPublic;
    }

    public Integer getNeedVer() {
        return needVer;
    }

    public void setNeedVer(Integer needVer) {
        this.needVer = needVer;
    }

    public String getDsAlias() {
        return dsAlias;
    }

    public void setDsAlias(String dsAlias) {
        this.dsAlias = dsAlias;
    }

    public String getDsName() {
        return dsName;
    }

    public void setDsName(String dsName) {
        this.dsName = dsName;
    }

    /**
     * 取得表间关联关系。
     *
     * @return
     */
    public String getRelation() {
        if(isExtTable()) {
            return relation;
        }
        return TableModel.FK_COLUMN_NAME;
    }

    public void setRelation(String relation) {
        this.relation = relation;
    }

    public Short getKeyType() {
        return keyType;
    }

    public void setKeyType(Short keyType) {
        this.keyType = keyType;
    }

    public String getKeyValue() {
        return keyValue;
    }

    public void setKeyValue(String keyValue) {
        this.keyValue = keyValue;
    }

    public String getPkField() {
        return pkField;
    }

    public void setPkField(String pkField) {
        this.pkField = pkField;
    }

    public String getListTemplate() {
        return listTemplate;
    }

    public void setListTemplate(String listTemplate) {
        this.listTemplate = listTemplate;
    }

    public String getDetailTemplate() {
        return detailTemplate;
    }

    public void setDetailTemplate(String detailTemplate) {
        this.detailTemplate = detailTemplate;
    }

    public Integer getGenByForm() {
        return genByForm;
    }

    public void setGenByForm(Integer genByForm) {
        this.genByForm = genByForm;
    }

    public String getTeam() {
        return team;
    }

    public void setTeam(String team) {
        this.team = team;
    }

    public Short getKeyDataType() {
        return keyDataType;
    }

    public void setKeyDataType(Short keyDataType) {
        this.keyDataType = keyDataType;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public Integer getHasForm() {
        return hasForm;
    }

    public void setHasForm(Integer hasForm) {
        this.hasForm = hasForm;
    }

    public String getSubStr() {
        return subStr;
    }

    public void setSubStr(String subStr) {
        this.subStr = subStr;
    }

    public List<FormTable> getOtherTableList() {
        return otherTableList;
    }

    public void setOtherTableList(List<FormTable> otherTableList) {
        this.otherTableList = otherTableList;
    }

    /**
     * 是否外部表
     *
     * @return true:是外部表;false:不是外部表
     */
    public boolean isExtTable() {
        return isExternal == EXTERNAL;
    }

    @Override
    public String toString() {
        return "FormTable{" +
                "tableId=" + tableId +
                ", tableName='" + tableName + '\'' +
                ", tableDesc='" + tableDesc + '\'' +
                ", isMain=" + isMain +
                ", mainTableId=" + mainTableId +
                ", isPublished=" + isPublished +
                ", publishedBy='" + publishedBy + '\'' +
                ", publishTime=" + publishTime +
                ", isExternal=" + isExternal +
                ", isPublic=" + isPublic +
                ", needVer=" + needVer +
                ", dsAlias='" + dsAlias + '\'' +
                ", dsName='" + dsName + '\'' +
                ", relation='" + relation + '\'' +
                ", keyType=" + keyType +
                ", keyValue='" + keyValue + '\'' +
                ", pkField='" + pkField + '\'' +
                ", listTemplate='" + listTemplate + '\'' +
                ", detailTemplate='" + detailTemplate + '\'' +
                ", genByForm=" + genByForm +
                ", team='" + team + '\'' +
                ", keyDataType=" + keyDataType +
                ", creator='" + creator + '\'' +
                ", categoryId=" + categoryId +
                ", subStr='" + subStr + '\'' +
                ", hasForm=" + hasForm +
                '}';
    }

    /**
     * 添加字段。
     *
     * <pre>
     * 	1.做重复新检测。
     *  2.重复字段返回失败。
     * </pre>
     *
     * @param field
     */
    public boolean addField(FormField field) {
        String fieldName = field.getFieldName().toLowerCase();
        short controlType = field.getControlType();

        if (hashSet.contains(fieldName)) {
            // 如果字段类型为复选框或者单选按钮的时候可以重复。
            if (controlType == 13 || controlType == 14) {
                return true;
            }
            return false;
        }
        ;
        hashSet.add(fieldName);
        this.fieldList.add(field);
        return true;
    }
    /**
     * 数据库真正的表名
     * @return
     */
    public String getFactTableName() {
        return this.isExtTable() ?tableName : TableModel.CUSTOMER_TABLE_PREFIX + this.tableName;
    }
}
